/*
 * smart_lock.cpp
 *
 *  Created on: 2012-7-5
 *      Author: ThinkPad
 */

#include "smart_lock.h"

smart_lock::smart_lock()
{
    pthread_mutex_init(&mutex,NULL);
    m_bLocked = FALSE;
}

smart_lock::~smart_lock()
{
    if(m_bLocked == TRUE)
    {
        pthread_mutex_unlock(&mutex);
        m_bLocked =  FALSE;
    }

    pthread_mutex_destroy(&mutex);
}

int smart_lock::Lock()
{
    int ret = 0;
    ret = pthread_mutex_lock(&mutex);
    if(ret == 0)
    {
        m_bLocked =  TRUE;
    }

    return ret;
}

int smart_lock::Unlock()
{
    int ret = 0;
    if(m_bLocked == TRUE)
    {
        m_bLocked =  FALSE;
        ret = pthread_mutex_unlock(&mutex);
    }

    return ret;
}
